//
//  RYVideoView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/14.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class RYVideoView: SYBaseView {

    
    var videoContent: UIImageView = {
        let v: UIImageView = UIImageView()
        
        v.isUserInteractionEnabled = true
        return v
    }()
    
    var tool: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        
        return v
    }()
    
    let chongbo: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "chongbo")
        return v
    }()
    
    let voice: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "voice")
        return v
    }()
    
    let quanping: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "quanping")
        return v
    }()
    
    let play: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "play_ry")
        return v
    }()
    
    
    let location: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "location")
        return v
    }()
    
    var titleLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 16.autoFontSize()
        v.textColor = UIColor(hex: 0x1D1D1D)
        
        return v
    }()
    
    var locationLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 13.autoFontSize()
        v.textColor = UIColor(hex: 0x999999)
        
        return v
    }()
    
    let playBt: UIButton = {
        let v: UIButton  = UIButton()
        
        return v
    }()
    
    let back: VGImageButton = {
        let v: VGImageButton  = VGImageButton()
        v.imageView.image = UIImage(named: "nav_back")
        return v
    }()
    
    func configure(_ model: RYProductDetailModel) {
        let urls:[String] = model.detailImgUrl?.components(separatedBy: ",") ?? []
        var arr:[String] = []
        for (_,value) in urls.enumerated() {
            arr.append("\(IMGIP)\(value)")
        }
        videoContent.kf.setImage(with: URL(string: arr.first ?? ""))
        titleLb.text = model.name
        locationLb.text = model.farmName
    }
    
    override func initViews() {
        
        playBt.addAction {
            let vc = RYVideoDetailViewController()
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        }
        back.btn.addAction {
            self.currentVC.navigationController?.popViewController(animated: true)
        }
        addSubview(videoContent)
        videoContent.addSubview(tool)
        videoContent.addSubview(play)
        videoContent.addSubview(playBt)
        tool.addSubview(chongbo)
        tool.addSubview(voice)
        tool.addSubview(quanping)
        addSubview(titleLb)
        addSubview(location)
        addSubview(locationLb)
        addSubview(sepView)
        videoContent.addSubview(back)
        videoContent.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(231.autoWidth())
        }
        tool.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(25.autoWidth())
        }
        play.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(29.autoWidth())
            make.height.equalTo(37.autoWidth())
        }
        playBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        chongbo.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-70.autoWidth())
            make.height.width.equalTo(17.autoWidth())
        }
        
        voice.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-41.autoWidth())
            make.height.width.equalTo(17.autoWidth())
        }
        
        quanping.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.width.equalTo(17.autoWidth())
        }
        titleLb.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(15.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        location.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(44.autoWidth())
            make.left.equalToSuperview().offset(16.autoWidth())
            make.height.equalTo(13.autoWidth())
            make.width.equalTo(11.autoWidth())
        }
        locationLb.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(44.autoWidth())
            make.left.equalToSuperview().offset(35.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(71.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
            make.bottom.equalToSuperview()
        }
        
        back.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(38.autoWidth())
        }
    }

}


/// RYVideoDetailView
class RYVideoDetailView: SYBaseView, SYPlayerDelegate {

    
    let fullScreenVC = SYFullScreenViewController()
    var player: SYPlayer!
    var videoContent: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        
        return v
    }()
    
    var tool: RYVideoToolView = {
        let v: RYVideoToolView = RYVideoToolView()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        v.isHidden = true
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        
        return v
    }()
    
    let play: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "zanting") // bofang
        v.isHidden = true
        return v
    }()
    
    
    let location: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "location")
        return v
    }()
    
    var titleLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 16.autoFontSize()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.text = "龙池矮脚黄"
        return v
    }()
    
    var locationLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 13.autoFontSize()
        v.textColor = UIColor(hex: 0x999999)
        v.text = "瑞康农场一号青菜基地"
        return v
    }()
    
    var lishiLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 15.autoFontSize()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.text = "历史视频"
        return v
    }()
    
//    deinit {
//        player!.pause()
//        player = nil
//    }
    
    override func initViews() {
        playVideo()
        addSubview(videoContent)
        videoContent.addSubview(tool)
        videoContent.addSubview(play)
        
        addSubview(titleLb)
        addSubview(location)
        addSubview(locationLb)
        addSubview(sepView)
        addSubview(lishiLb)
        addSubview(lineView)
        videoContent.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(231.autoWidth())
        }
        tool.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(25.autoWidth())
        }
        play.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(37.autoWidth())
            make.height.equalTo(37.autoWidth())
        }
        titleLb.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(15.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        location.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(44.autoWidth())
            make.left.equalToSuperview().offset(16.autoWidth())
            make.height.equalTo(13.autoWidth())
            make.width.equalTo(11.autoWidth())
        }
        locationLb.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(44.autoWidth())
            make.left.equalToSuperview().offset(35.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(71.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
            
        }
        lishiLb.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(97.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(videoContent.snp.bottom).offset(123.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(0.5.autoWidth())
            make.bottom.equalToSuperview()
        }
    }

    func playVideo() {
        
        let model = WMPlayerModel()
        model.title = ""
        model.videoURL = URL(string: testMP4)
        player = SYPlayer(model: model)
        self.videoContent.addSubview(player)
        // 不能用约束，后面改变frame有问题
        let originalFrame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 231.autoWidth())
        player.frame = originalFrame
        player.play()
        player.delegate = self
        
        fullScreenVC.syPlayer = player
        fullScreenVC.originalFrame = originalFrame
        fullScreenVC.superView = self.videoContent
    }
    
    
    func wmplayer(_ wmplayer: SYPlayer!, clickedFullScreenButton fullScreenBtn: UIButton!) {
        
        
        if fullScreenBtn.isSelected == true {

            fullScreenVC.fullScreenAction()
            self.currentVC.sy_push(fullScreenVC, false)
        } else {
            fullScreenVC.backAction()
        }
    }

    func wmplayer(_ wmplayer: SYPlayer!, clickedClose backBtn: UIButton!) {
        fullScreenVC.backAction()
    }
}
